package Websites;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLEncoder;

import KUtilities.KLogger;

public class _160By2 extends SMSService{
	
    private String action1;
    
    public _160By2(KLogger logger){
    	super(logger,"160by2.com");
    	setSendData("http://160by2.com/SendSMSAction","http://160by2.com/SendSMS","Your message has been Sent");
    	setLoginData("http://160by2.com/re-login","http://160by2.com/");
    }
    
    protected String getLoginContent(String username, String password) {
		String loginContent;
		loginContent="username="+username;
		loginContent+="&password="+password;
		loginContent+="&button=Login";
		return loginContent;
	}

    protected boolean isValidLogin(HttpURLConnection loginConnection){
    	String location=loginConnection.getHeaderField("Location");
    	if(location==null || location.indexOf("SendSMS.action;jsessionid=")==-1)
        	return false;
		return true;
	}
    
	protected void grabRequirements() {
		thread=new Thread(){
        	public void run(){
        		try{
        			int temp_index=cookie.indexOf("JSESSIONID=");
        			String loc=cookie.substring(cookie.indexOf("~",temp_index)+1,cookie.indexOf(";"));
        			URL location=new URL("http://160by2.com/SendSMS?id="+loc);
        			sendReferrer="http://160by2.com/SendSMS?id="+loc;
        			HttpURLConnection actionConnection=(HttpURLConnection)location.openConnection();
                    actionConnection.setRequestProperty("Host",host);
                    actionConnection.setRequestProperty("User-Agent","Mozilla/5.0 (X11; Linux x86_64; rv:7.0.1) Gecko/20100101 Firefox/7.0.1");
                    actionConnection.setRequestProperty("Accept","text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8");
                    actionConnection.setRequestProperty("Accept-Language","en-us,en;q=0.5");
                    actionConnection.setRequestProperty("Accept-Charset","ISO-8859-1,utf-8;q=0.7,*;q=0.7");
                    actionConnection.setRequestProperty("Connection","keep-alive");
                    actionConnection.setRequestProperty("Cookie", cookie);
                    actionConnection.setRequestMethod("GET");
                    actionConnection.setInstanceFollowRedirects(false);
                    BufferedReader actionReader=new BufferedReader(new InputStreamReader(actionConnection.getInputStream()));
                    StringBuilder response =new StringBuilder();
                    String temp="";
                    while((temp=actionReader.readLine())!=null){
                        response.append(temp+"\n");
                    }
                    actionReader.close();
                    String searchStr="<input type=\"hidden\" id=\"action1\" name=\"action1\" value=";
                    int i=response.indexOf(searchStr)+searchStr.length()+1;
                    action1=URLEncoder.encode(response.substring(i,response.indexOf("\"", i)),"UTF-8");
        		}catch(Exception e){
        			e.printStackTrace();
        		}
        	}
        };
        thread.start();
	}
    
    protected String getSendContent(String number,String message)
    {
    	String sendContent;
        sendContent="hid_exists=no";
        sendContent+="&action1="+action1;
        sendContent+="&mobile1="+number;
        sendContent+="&msg1="+message;
        sendContent+="&sel_month=0";
        sendContent+="&sel_day=0";
        sendContent+="&sel_year=0";
        sendContent+="&sel_hour=hh";
        sendContent+="&sel_minute=mm";
        sendContent+="&txt_sendsms_count="+(getMaxChar()-message.length())+" characters remaining";
        return sendContent;
    }
        
    public boolean logout()
    {
    	return logout("http://sms440.com/logout.aspx");
    }
    
	public String getServiceName() {
		return "160by2";
	}

	public int getMaxChar() {
		return 140;
	}
}
